package com.one.blocks.rbac.jetcache.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;


/**
 * web接口转换可以通过mapstruct，这里是为了方便缓存的序列化
 *
 * @author <a href="mailto:idler41@163.com">linfuxin</a> created by 2022-02-20 20:27:20
 */
public class LocalDateTimeToLong extends StdSerializer<LocalDateTime> {

    public LocalDateTimeToLong() {
        super(LocalDateTime.class);
    }

    @Override
    public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        if (value == null) {
            gen.writeNull();
            return;
        }
        gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
    }
}
